package pers.jing.android.helper.page

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import pers.jing.android.helper.Device
import pers.jing.android.helper.DeviceList
import pers.jing.android.helper.ParamConfig
import pers.jing.android.helper.Settings
import pers.jing.android.helper.model.UICloseSate
import pers.jing.android.helper.widget.ConfirmCancelDialog
import pers.jing.android.helper.widget.ConfirmDialog
import pers.jing.android.helper.widget.InstallApkDialog

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainPage(
    closState: UICloseSate,
    onConfirmClose: () -> Unit,
    onCancelClose: () -> Unit,
    paramConfig: ParamConfig,
    onClearParam: () -> Unit,
) {
    //默认进入设备列表
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = DeviceList) {
        //设备列表
        composable<DeviceList> {
            DeviceListPage(onClickSettings = {
                navController.navigate(Settings)
            }, onClickDevice = {
                navController.navigate(Device(it))
            })
        }
        composable<Settings> {
            SettingsPage(onClickBack = {
                //返回
                navController.popBackStack(route = DeviceList, inclusive = false)
            })
        }
        composable<Device> { entry ->
            val device = entry.toRoute<Device>()
            DevicePage(device.serialId, onClickBack = {
                //返回
                navController.popBackStack(route = DeviceList, inclusive = false)
            })
        }
    }
    if (UICloseSate.PrepareClose == closState) {
        //准备关闭
        ConfirmCancelDialog(
            msg = "您确认要关闭“Android手机助手”吗？",
            onConfirm = {
                //退出应用之前一些事情
                onConfirmClose()
            }, onDismiss = {
                onCancelClose()
            })
    }

    if (paramConfig.install?: false) {
        //安装
        if (paramConfig.apkPath.isNullOrEmpty()) {
            ConfirmDialog(
                msg = "安装失败，未设置apk路径，命令格式如下：\n" +
                        "xxx.exe --install xxx/xxx.apk",
                onConfirm = {
                    onClearParam()
                }
            ) {
                onClearParam()
            }
        } else {
            //显示安装弹窗
            InstallApkDialog(
                apkPath = paramConfig.apkPath!!,
                onDismiss = {
                    onClearParam()
                }
            )
        }
    }


}